<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

// Clear remember me cookie if exists
if (isset($_COOKIE['remember_token'])) {
    // Clear token from database
    $stmt = $pdo->prepare("UPDATE users SET remember_token = NULL WHERE remember_token = ?");
    $stmt->execute([$_COOKIE['remember_token']]);
    
    // Clear cookie
    setcookie('remember_token', '', time() - 3600, '/');
}

// Clear all session variables
$_SESSION = array();

// Destroy the session cookie
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Destroy session
session_destroy();

// Redirect to home page with success message
header('Location: /chuan/535g/?logged_out=1');
exit();
?>
